package com.quicklife.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.quicklife.pojo.Business;
import com.quicklife.pojo.Coupons;
import com.quicklife.pojo.Tuser;
import com.quicklife.service.inter.BussInfoManageService;
import com.quicklife.service.inter.FileUploadService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 修改商家信息
 * 
 * @author HackerD
 * 
 */
public class BussInfoManageServiceImpl implements BussInfoManageService {

	@Override
	public String modifyPassword(String rqinfos) {
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			SqlHelper.executeUpdate(
					"update business set password=? where id=?",
					new String[] { jsonObject.getString("password"),
							jsonObject.getString("userid") });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					StringUtil.RESULT_SUCESS_UPDATE);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String modifyBussInfos(HttpServletRequest request) {
		try {
			FileUploadService fileUploadService = new FileUploadServiceImpl();
			String tempInfos = fileUploadService.getUploadInfos(request);
			// JSONObject jsonObject = JSONObject.fromObject(tempInfos);
			Business buss = (Business) JsonUtil.toBean(tempInfos,
					Business.class);
			// if (jsonObject.containsKey("logo")) {
			// buss.setLogo(jsonObject.getString("logo"));
			// }

			String sql = "update business set type=?, name=?, phone=?, logo=?, lat=?, lon=? where id=?";
			String[] params = new String[] {
					StringUtil.toString(buss.getType()), buss.getName(),
					buss.getPhone(), buss.getLogo(),
					StringUtil.toString(buss.getLat()),
					StringUtil.toString(buss.getLon()),
					StringUtil.toString(buss.getId()) };

			// 判断如果logo为空的话 不更新
			if (StringUtil.isNull(buss.getLogo())) {
				sql = "update business set type=?, name=?, phone=?,  lat=?, lon=? where id=?";
				params = new String[] { StringUtil.toString(buss.getType()),
						buss.getName(), buss.getPhone(),
						StringUtil.toString(buss.getLat()),
						StringUtil.toString(buss.getLon()),
						StringUtil.toString(buss.getId()) };
			}

			// 更新资料
			SqlHelper.executeUpdate(sql, params);

			// 保存Logo
			fileUploadService.saveFile();

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					StringUtil.RESULT_SUCESS_UPDATE);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getBusInfos(String busid) {
		try {
			List list = SqlHelper.executeQuery(
					"select count(*) total,avg(b.score) score from coupons a inner join business b "
							+ "on a.busid=b.id where busid=?",
					new String[] { busid }, null);
			
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}
}
